home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / popen2.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  8.8 KB  |  237 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. """Spawn a command with pipes to its stdin, stdout, and optionally stderr.
  5.  
  6. The normal os.popen(cmd, mode) call spawns a shell command and provides a
  7. file interface to just the input or output of the process depending on
  8. whether mode is 'r' or 'w'.  This module provides the functions popen2(cmd)
  9. and popen3(cmd) which return two or three pipes to the spawned command.
  10. """
  11. import os
  12. import sys
  13. import types
  14. __all__ = [
  15.     'popen2',
  16.     'popen3',
  17.     'popen4']
  18. MAXFD = 256
  19. _active = []
  20.  
  21. def _cleanup():
  22.     for inst in _active[:]:
  23.         inst.poll()
  24.     
  25.  
  26.  
  27. class Popen3:
  28.     '''Class representing a child process.  Normally instances are created
  29.     by the factory functions popen2() and popen3().'''
  30.     sts = -1
  31.     
  32.     def __init__(self, cmd, capturestderr = 0, bufsize = -1):
  33.         """The parameter 'cmd' is the shell command to execute in a
  34.         sub-process.  The 'capturestderr' flag, if true, specifies that
  35.         the object should capture standard error output of the child process.
  36.         The default is false.  If the 'bufsize' parameter is specified, it
  37.         specifies the size of the I/O buffers to/from the child process."""
  38.         _cleanup()
  39.         (p2cread, p2cwrite) = os.pipe()
  40.         (c2pread, c2pwrite) = os.pipe()
  41.         if capturestderr:
  42.             (errout, errin) = os.pipe()
  43.         
  44.         self.pid = os.fork()
  45.         if self.pid == 0:
  46.             os.dup2(p2cread, 0)
  47.             os.dup2(c2pwrite, 1)
  48.             if capturestderr:
  49.                 os.dup2(errin, 2)
  50.             
  51.             self._run_child(cmd)
  52.         
  53.         os.close(p2cread)
  54.         self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
  55.         os.close(c2pwrite)
  56.         self.fromchild = os.fdopen(c2pread, 'r', bufsize)
  57.         if capturestderr:
  58.             os.close(errin)
  59.             self.childerr = os.fdopen(errout, 'r', bufsize)
  60.         else:
  61.             self.childerr = None
  62.         _active.append(self)
  63.  
  64.     
  65.     def _run_child(self, cmd):
  66.         if isinstance(cmd, types.StringTypes):
  67.             cmd = [
  68.                 '/bin/sh',
  69.                 '-c',
  70.                 cmd]
  71.         
  72.         for i in range(3, MAXFD):
  73.             
  74.             try:
  75.                 os.close(i)
  76.             except:
  77.                 pass
  78.  
  79.         
  80.         
  81.         try:
  82.             os.execvp(cmd[0], cmd)
  83.         finally:
  84.             os._exit(1)
  85.  
  86.  
  87.     
  88.     def poll(self):
  89.         """Return the exit status of the child process if it has finished,
  90.         or -1 if it hasn't finished yet."""
  91.         if self.sts < 0:
  92.             
  93.             try:
  94.                 (pid, sts) = os.waitpid(self.pid, os.WNOHANG)
  95.                 if pid == self.pid:
  96.                     self.sts = sts
  97.                     _active.remove(self)
  98.             except os.error:
  99.                 pass
  100.  
  101.         
  102.         return self.sts
  103.  
  104.     
  105.     def wait(self):
  106.         '''Wait for and return the exit status of the child process.'''
  107.         (pid, sts) = os.waitpid(self.pid, 0)
  108.         if pid == self.pid:
  109.             self.sts = sts
  110.             _active.remove(self)
  111.         
  112.         return self.sts
  113.  
  114.  
  115.  
  116. class Popen4(Popen3):
  117.     childerr = None
  118.     
  119.     def __init__(self, cmd, bufsize = -1):
  120.         _cleanup()
  121.         (p2cread, p2cwrite) = os.pipe()
  122.         (c2pread, c2pwrite) = os.pipe()
  123.         self.pid = os.fork()
  124.         if self.pid == 0:
  125.             os.dup2(p2cread, 0)
  126.             os.dup2(c2pwrite, 1)
  127.             os.dup2(c2pwrite, 2)
  128.             self._run_child(cmd)
  129.         
  130.         os.close(p2cread)
  131.         self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
  132.         os.close(c2pwrite)
  133.         self.fromchild = os.fdopen(c2pread, 'r', bufsize)
  134.         _active.append(self)
  135.  
  136.  
  137. if sys.platform[:3] == 'win':
  138.     del Popen3
  139.     del Popen4
  140.     
  141.     def popen2(cmd, bufsize = -1, mode = 't'):
  142.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  143.         specified, it sets the buffer size for the I/O pipes.  The file objects
  144.         (child_stdout, child_stdin) are returned."""
  145.         (w, r) = os.popen2(cmd, mode, bufsize)
  146.         return (r, w)
  147.  
  148.     
  149.     def popen3(cmd, bufsize = -1, mode = 't'):
  150.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  151.         specified, it sets the buffer size for the I/O pipes.  The file objects
  152.         (child_stdout, child_stdin, child_stderr) are returned."""
  153.         (w, r, e) = os.popen3(cmd, mode, bufsize)
  154.         return (r, w, e)
  155.  
  156.     
  157.     def popen4(cmd, bufsize = -1, mode = 't'):
  158.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  159.         specified, it sets the buffer size for the I/O pipes.  The file objects
  160.         (child_stdout_stderr, child_stdin) are returned."""
  161.         (w, r) = os.popen4(cmd, mode, bufsize)
  162.         return (r, w)
  163.  
  164. else:
  165.     
  166.     def popen2(cmd, bufsize = -1, mode = 't'):
  167.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  168.         specified, it sets the buffer size for the I/O pipes.  The file objects
  169.         (child_stdout, child_stdin) are returned."""
  170.         inst = Popen3(cmd, 0, bufsize)
  171.         return (inst.fromchild, inst.tochild)
  172.  
  173.     
  174.     def popen3(cmd, bufsize = -1, mode = 't'):
  175.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  176.         specified, it sets the buffer size for the I/O pipes.  The file objects
  177.         (child_stdout, child_stdin, child_stderr) are returned."""
  178.         inst = Popen3(cmd, 1, bufsize)
  179.         return (inst.fromchild, inst.tochild, inst.childerr)
  180.  
  181.     
  182.     def popen4(cmd, bufsize = -1, mode = 't'):
  183.         """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
  184.         specified, it sets the buffer size for the I/O pipes.  The file objects
  185.         (child_stdout_stderr, child_stdin) are returned."""
  186.         inst = Popen4(cmd, bufsize)
  187.         return (inst.fromchild, inst.tochild)
  188.  
  189.     __all__.extend([
  190.         'Popen3',
  191.         'Popen4'])
  192.  
  193. def _test():
  194.     cmd = 'cat'
  195.     teststr = 'ab cd\n'
  196.     if os.name == 'nt':
  197.         cmd = 'more'
  198.     
  199.     expected = teststr.strip()
  200.     print 'testing popen2...'
  201.     (r, w) = popen2(cmd)
  202.     w.write(teststr)
  203.     w.close()
  204.     got = r.read()
  205.     if got.strip() != expected:
  206.         raise ValueError('wrote %s read %s' % (`teststr`, `got`))
  207.     
  208.     print 'testing popen3...'
  209.     
  210.     try:
  211.         (r, w, e) = popen3([
  212.             cmd])
  213.     except:
  214.         (r, w, e) = popen3(cmd)
  215.  
  216.     w.write(teststr)
  217.     w.close()
  218.     got = r.read()
  219.     if got.strip() != expected:
  220.         raise ValueError('wrote %s read %s' % (`teststr`, `got`))
  221.     
  222.     got = e.read()
  223.     if got:
  224.         raise ValueError('unexected %s on stderr' % `got`)
  225.     
  226.     for inst in _active[:]:
  227.         inst.wait()
  228.     
  229.     if _active:
  230.         raise ValueError('_active not empty')
  231.     
  232.     print 'All OK'
  233.  
  234. if __name__ == '__main__':
  235.     _test()
  236.  
  237.